<template>
  <div>
    <div class="myinput">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input
            type="text"
            v-model="ruleForm.name"
            autocomplete="off"
            @blur="findName()"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <textarea
            cols="24"
            rows="3"
            v-model.number="ruleForm.description"
          ></textarea>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-show="bool" @click="submitForm('ruleForm')"
            >添加分类</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <hr />

    <el-tag
    class="mytags"
      :key="tag"
      v-for="tag in dynamicTags"
      closable
      :disable-transitions="false"
      @close="handleClose(tag)"
    >
      {{ tag.name }}
    </el-tag>
    <el-input
      class="button-new-ta"
      v-if="inputVisible"
      v-model="inputValue"
      ref="saveTagInput"
      size="small"
      @keyup.enter.native="handleInputConfirm"
      @blur="handleInputConfirm"
    >
    </el-input>
  </div>
</template>


<script>
import CategoryApi from "@/api/Category";
export default {
  data() {
    
      
    return {
      dynamicTags: [],

      inputVisible: false,
      inputValue: "",

      ruleForm: {
        name: "",
        description: "",
      },
      bool: true
    };
  },
  created() {
    this.findList();
  },
  methods: {
    findList() {
      CategoryApi.findList().then((res) => {
        // console.log(res)
        this.dynamicTags = res.data;
      });
    },

    // ========================================
    handleClose(tag) {
      // console.log("==========="+tag.categoryId)

      // 删除元素
      CategoryApi.delete(tag.categoryId).then((res) => {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      });
    },

    //重置按钮
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    //添加按钮
    submitForm(formName){

      CategoryApi.addCategory(this.ruleForm).then(res=>{
        this.$refs[formName].resetFields();
        this.$router.go(0)
      }).catch(error=>{
        console.log("====********=")
        this.bool=false
      })
      
    },
    //验证名字是否重复
    findName(){
      console.log("**********")
      CategoryApi.findName_(this.ruleForm).then((res)=>{
       this.bool=true
      }).catch(error=>{
        console.log("====********=")
        this.bool=false
      })
    }
  },
};
</script>

<style>

.el-tag + .el-tag {
  margin-left: 25px;
}
.button-new-tag {
  background-color: black;
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

.myinput {
  width: 300px;
}
.mytags{
  font-size: 50px;
  padding: 20px;
  padding-bottom: 50px;
}
</style>